package com.stylefeng.guns.modular.salesmanagement.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.CustomerReturnGoodsAddRequest;
import com.stylefeng.guns.modular.dto.CustomerReturnGoodsQueryRequest;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.stylefeng.guns.modular.salesmanagement.service.ICustomerReturnListService;
import com.stylefeng.guns.modular.salesmanagement.service.ICustomerReturnService;
import com.stylefeng.guns.modular.system.dao.CustomerReturnMapper;
import com.stylefeng.guns.modular.system.model.CustomerReturn;
import com.stylefeng.guns.modular.system.model.CustomerReturnList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
@Transactional(readOnly = true,rollbackFor = RuntimeException.class)
public class CustomerReturnServiceImpl extends ServiceImpl<CustomerReturnMapper, CustomerReturn> implements ICustomerReturnService {

    @Autowired
    private ICustomerReturnListService customerReturnListService;

    @Autowired
    private IManagerGoodsService managerGoodsService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean insertCustomerReturnListAndGoods(CustomerReturnGoodsAddRequest request) {
        CustomerReturn customerReturn = new CustomerReturn();
        BeanUtils.copyProperties(request, customerReturn);
        boolean success = this.insert(customerReturn);
        //先取出商品id和本次要增加的商品数量
        Map<Long, Integer> tempMap = Maps.newHashMap();
        request.getReturnSaleGoodsList().forEach(customerReturnList -> {
            customerReturnList.setCustomerReturnId(customerReturn.getId());
            tempMap.put(customerReturnList.getGoodsId(), customerReturnList.getReturnGoodsNumber());
        });
        managerGoodsService.batchAddGoodsInventory(tempMap);
        boolean insertCustomerReturnList = customerReturnListService.insertBatch(request.getReturnSaleGoodsList());
        return success && insertCustomerReturnList;
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean deleteCustomerReturnListAndGoods(Long customerReturnId) {
        EntityWrapper<CustomerReturnList> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("customer_return_id", customerReturnId);
        List<CustomerReturnList> returnListList = customerReturnListService.selectList(entityWrapper);
        //更新库存数量
        Map<Long,Integer> tempMap = returnListList.stream().collect(Collectors.toMap(CustomerReturnList::getGoodsId,CustomerReturnList::getReturnGoodsNumber));
        managerGoodsService.batchAddGoodsInventory(tempMap);
        boolean success = customerReturnListService.delete(entityWrapper);
        boolean deleteCustomerReturn = this.deleteById(customerReturnId);
        return success && deleteCustomerReturn;
    }

    @Override
    public Page<CustomerReturn> selectPageWithDataScope(Page<CustomerReturn> page, CustomerReturnGoodsQueryRequest request) {
        EntityWrapper<CustomerReturn> entityWrapper = new EntityWrapper<>();
        //订单号
        if(StringUtils.isNotBlank(request.getReturnOrderNumber())){
            entityWrapper.eq("return_order_number", request.getReturnOrderNumber());
        }

        //是否支付
        if(StringUtils.isNotBlank(request.getPayState())){
            entityWrapper.eq("pay_state", Integer.valueOf(request.getPayState()));
        }

        //开始日期 大于等于
        if(StringUtils.isNotBlank(request.getbDate())){
            entityWrapper.ge("create_date", DateUtil.parse(request.getbDate(),"yyyy-MM-dd"));
        }

        //结束日期 小于等于
        if(StringUtils.isNotBlank(request.geteDate())){
            entityWrapper.le("create_date", DateUtil.parse(request.geteDate(),"yyyy-MM-dd"));
        }

        //根据备注信息查询
        if(StringUtils.isNotBlank(request.getRemarks())){
            entityWrapper.like("remarks", request.getRemarks());
        }

        if(ShiroKit.isAdmin()){
            Page<CustomerReturn> result = this.selectPage(page,entityWrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<CustomerReturn> result = this.baseMapper.selectPageWithDataScope(page,dataScope,request);
            page.setRecords(result);
            return page;
        }
    }
}
